package com.ahua.mallchat.common.dao;

import com.ahua.mallchat.common.domain.enums.system.YesOrNoEnum;
import com.ahua.mallchat.common.domain.pojo.User;
import com.ahua.mallchat.common.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDao extends ServiceImpl<UserMapper, User> {

    /**
     * 根据openId获取用户
     *
     * @param openId
     * @return
     */
    public User getByOpenId(String openId) {
        return lambdaQuery().eq(User::getOpenId, openId).one();
    }

    public User getByName(String name) {
        return lambdaQuery().eq(User::getName, name).one();
    }

    public void modifyName(Long uid, String name) {
        lambdaUpdate().eq(User::getId, uid).set(User::getName, name).update();
    }

    public void wearingBadge(Long uid, Long itemId) {
        lambdaUpdate().eq(User::getId, uid).set(User::getItemId, itemId).update();
    }

    /**
     * 修改用户状态为拉黑状态
     *
     * @param uid
     */
    public void invalidUid(Long uid) {
        lambdaUpdate().eq(User::getId, uid).set(User::getStatus, YesOrNoEnum.YES.getStatus()).update();
    }

    /**
     * 根据uid查询用户id 头像 用户名 在线状态
     */
    public List<User> getFriendList(List<Long> friendUids) {
        return lambdaQuery()
                .in(User::getId, friendUids)
                .select(User::getId, User::getAvatar, User::getName, User::getActiveStatus)
                .list();
    }
}
